package com.cyj.controller;

import com.cyj.annotation.UserLoginToken;
import com.cyj.dto.SearchDto;
import com.cyj.pojo.Shop;
import com.cyj.service.shop.ShopService;

import com.cyj.utils.constants.Constants;
import com.cyj.utils.response.JsonObject;
import com.cyj.utils.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyj
 * @since 2022-04-18
 */
@CrossOrigin
@Api(tags = {"商品记录"})
@RestController
@RequestMapping("/shop")
public class ShopController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private ShopService shopService;


    @ApiOperation(value = "查询记录")
    @PostMapping("/queryAll")
    public JsonObject<Shop> queryAll(@RequestBody  SearchDto searchDto){
        JsonObject<Shop> jsonObject=new JsonObject<>();
        try{
            jsonObject=shopService.queryShopList(searchDto);
            jsonObject.setCode(Constants.OK_CODE);

        }catch (Exception e){
            e.printStackTrace();
            jsonObject.setCode(Constants.FAIL_CODE);
        }
        return jsonObject;
    }


    @ApiOperation(value = "新增 " )
    @PostMapping("/add")
    public R add(@RequestBody Shop shop){
        try{
            if(shopService.add(shop)!=1){
                return R.fail(Constants.FAIL_MSG);
            }
            return R.ok();
        }catch (Exception e){
            return R.fail(Constants.FAIL_MSG);
        }
    }


    @ApiOperation(value = "删除")
    @ApiImplicitParams({

    })
    @PostMapping("/delete")
    public R delete(long id){
        try{
            if(shopService.delete(id)!=1){
                return R.fail(Constants.FAIL_MSG);
            }
            return R.ok();
        }catch (Exception e){
            return R.fail(Constants.FAIL_MSG);
        }
    }

    @ApiOperation(value = "更新" )
    @PostMapping("/update")
    public R update(@RequestBody Shop shop){
        try{
            if( shopService.updateData(shop)!=1){
                return R.fail(Constants.FAIL_MSG);
            }
            return R.ok();
        }catch (Exception e){
            return R.fail(Constants.FAIL_MSG);
        }
    }

}
